package com.edu.zut.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.zut.domain.CheckVo;
import com.edu.zut.domain.PaperResultVo;
import com.edu.zut.entity.Result;
import com.edu.zut.entity.User;
import com.edu.zut.mapper.ResultMapper;
import com.edu.zut.mapper.RoleMapper;
import com.edu.zut.mapper.UserMapper;
import com.edu.zut.result.HttpCode;
import com.edu.zut.result.ResponseResult;
import com.edu.zut.service.ResultService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 许文科
 * @since 2023-02-20
 */
@Service
public class ResultServiceImpl extends ServiceImpl<ResultMapper, Result> implements ResultService {

    @Resource
    private ResultMapper resultMapper;
    @Resource
    private UserMapper userMapper;

    @Resource
    private RoleMapper roleMapper;

    @Override
    public ResponseResult getResultList(String username) {
        //查找出当前用户信息
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        User user = userMapper.selectOne(wrapper.eq("user_name", username));
        List<String> roleNames = roleMapper.getRoleNamesbyUserId(user.getUserId());

        List<PaperResultVo> resultVoList = null;

        if (roleNames.contains("ROLE_admin")) {//admin
            resultVoList = resultMapper.getResultAllList();
        } else if (roleNames.contains("ROLE_teacher")) {//teacher
            resultVoList = resultMapper.getResultAllCollegeList(user.getUserId());
        } else {//ROLE_student
            resultVoList = resultMapper.getResult(user.getUserId());
        }

        if (!resultVoList.isEmpty()) {
            return new ResponseResult(HttpCode.SUCCESS, "成功获取数据", resultVoList);

        }
        return new ResponseResult(HttpCode.ERROR, "获取数据失败", resultVoList);

    }

    @Override
    public ResponseResult delete(Long resultId) {
        QueryWrapper<Result> wrapper = new QueryWrapper<>();
        wrapper.eq("result_id", resultId);
        Result result = resultMapper.selectOne(wrapper);

        if (result.getDelFlag().intValue() == 0) {
            result.setDelFlag(1);
            int i = resultMapper.updateById(result);
            if (i == 1) {
                return new ResponseResult(HttpCode.SUCCESS, "删除成功.", null);
            }
        }
        return new ResponseResult(HttpCode.ERROR, "删除失败.", null);
    }

    @Override
    public ResponseResult check(CheckVo checkVo) {
        QueryWrapper<Result> wrapper = new QueryWrapper<>();
        wrapper.eq("result_id", checkVo.getResultId());
        int flag = resultMapper.updataSuggestByResultId(checkVo.getResultId(), checkVo.getSuggest());
        if (flag == 1)
            return new ResponseResult(HttpCode.SUCCESS, "审核成功", null);
        else
            return new ResponseResult(HttpCode.ERROR, "审核失败", null);
    }

    @Override
    public ResponseResult find(String userName) {

        List<PaperResultVo> list = resultMapper.find(userName);
        if (!list.isEmpty())
            return new ResponseResult(HttpCode.SUCCESS, "查找成功", list);

        return new ResponseResult(HttpCode.ERROR, "为空", list);
    }
}
